<template>
  <div class="app-container">
    <el-card>
        <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-input
            v-model="keyWord"
            placeholder="请输入想要查询的信息"
            @keyup.enter.native.prevent="searchSCByName"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="searchSCByName"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>

      <!--SC table-->
      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        stripe
        height="550"
        border
        fits
        highlight-current-row
      >
        <el-table-column align="center" label="学号" prop="sId" />
        <el-table-column label="学生名称" prop="sName" />
        <el-table-column label="性别" align="center" prop="sSex" />
        <el-table-column label="年龄" align="center" prop="sAge" />
        <el-table-column label="课程编号" align="center" prop="cId" />
        <el-table-column label="课程名称" align="center" prop="cName" />
        <el-table-column label="学院编号" align="center" prop="deptId" />
        <el-table-column label="学院名" align="center" prop="deptName" />
        <el-table-column label="学院地址" align="center" prop="address" />
        <el-table-column label="分数" align="center">
          <template slot-scope="{ row }">
            <el-input v-if="row.isEdit" v-model="row.score" size="small"></el-input>
            <span v-else>{{ row.score }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="{ row }">
            <el-tag v-if="isRating(row.status)" :type="row.status | isRating">{{
              row.status
            }}</el-tag>
            <el-tag v-else :type="row.status | isRating">{{ row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="{ row }">
            <el-button
              v-show="!row.isEdit"
              type="primary"
              icon="el-icon-edit"
              circle
              size="small"
              @click="row.isEdit = !row.isEdit"
            ></el-button>
            <el-button
              v-show="row.isEdit"
              type="success"
              icon="el-icon-check"
              circle
              size="small"
              @click="rating(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    
  </div>
</template>

<script>
import { getTeachCourseInfo,teachRating } from "@/api/teacher";
export default {
  data() {
    return {
      list: null,
      listLoading: true,
      keyWord: "",
      dialogAddVisible: false,
    };
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getTeachCourseInfo(this.username).then((response) => {
        this.list = response.data.map((v) => {
          this.$set(v, "isEdit", false);
          return v;
        });
        this.listLoading = false;
      });
    },
    rating(row){
      row.isEdit = false;
      let {sId,cId,score} = row;
      teachRating({sId,cId,score}).then(resp=>{
        console.log(resp)
        if(resp.status == 200){
          this.$message({
            message: resp.msg,
            type: 'success'
          })
          row.status = '已评分';
        }
        else
           this.$message({
            message: resp.msg,
            type: 'danger'
          })
      })
    },
    isRating(val) {
      return val === "未评分";
    },
    searchSCByName() {},
  },
  filters: {
    isRating(val) {
      return val == "未评分" ? "danger" : "success";
    },
  },
  computed: {
    username() {
      return this.$store.getters.name;
    },
  },
  mounted() {
    this.fetchData();
  },
};
</script>
